package com.iweb.dishes.dao;

import com.iweb.dishes.model.Dishes;
import com.iweb.util.JdbcUtil;
import com.iweb.util.PageModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DishesDaoImpl implements DishesDao{
    @Override
    public List<Dishes> selectByPage(PageModel pageModel) {
        List<Dishes> dishes=new ArrayList<>();
        List<Map<String,Object>> list=null;
        String likeValue=pageModel.getLikeValue()==null? "":pageModel.getLikeValue();
        StringBuffer sql=new StringBuffer("select cid,dish_name,dish_type,dish_description,price from dishes where dish_name like ? or dish_type like ? or dish_description like ? or price like ?");
        int recordCount= JdbcUtil.jdbcBySelect(sql.toString(),"%"+likeValue+"%","%"+likeValue+"%","%"+likeValue+"%","%"+likeValue+"%").size();
        pageModel.setRecordCount(recordCount);
        int startCount=(pageModel.getPageNo()-1)*pageModel.getPageSize();
        sql.append(" order by price asc limit ?,?");
        list=JdbcUtil.jdbcBySelect(sql.toString(),"%"+likeValue+"%","%"+likeValue+"%","%"+likeValue+"%","%"+likeValue+"%",startCount,pageModel.getPageSize());
        if(list!=null&&list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                Map<String,Object> map=list.get(i);
                Integer cid=(Integer)map.get("cid");
                Integer price=(Integer)map.get("price");
                String dish_name=(String)map.get("dish_name");
                String dish_type=(String)map.get("dish_type");
                String dish_description=(String)map.get("dish_description");
                Dishes dish=new Dishes(cid,dish_name,dish_type,dish_description,price);
                dishes.add(dish);

            }
        }
        return dishes;
    }
//增加菜品
    @Override
    public int addOne(Dishes dishes) {
        String sql="insert into dishes values(null,?,?,?,?)";
        return JdbcUtil.jdbcByUpdate(sql,dishes.getDish_name(), dishes.getDish_type(), dishes.getDish_description(),dishes.getPrice());
    }
//更新菜品
    @Override
    public int updateOne(Dishes dishes) {
        String sql="update dishes set dish_name=?,dish_type=?,dish_description=?,price=? where cid=?";
        return JdbcUtil.jdbcByUpdate(sql,dishes.getDish_name(),dishes.getDish_type(),dishes.getDish_description(),dishes.getPrice(),dishes.getCid());
    }

    @Override
    public int deleteById(int cid) {
        String sql="delete from dishes where cid=?";
        return JdbcUtil.jdbcByUpdate(sql,cid);
    }

    @Override
    public Dishes queryById(String cid) {
        String sql="select cid,dish_name,dish_type, dish_description,price from dishes where cid=?";
        List<Map<String,Object>> list=JdbcUtil.jdbcBySelect(sql,cid);
        Map<String,Object> map=list.get(0);
        String dish_name=(String)map.get("dish_name");
        String dish_type=(String)map.get("dish_type");
        String dish_description=(String)map.get("dish_description");
        Integer price=(Integer)map.get("price");
        Dishes dishes=new Dishes(Integer.valueOf(cid),dish_name,dish_type,dish_description,price);
        return dishes;
    }
}
